//
//  EnsureAddressView.m
//  夺宝吧
//
//  Created by xiaocai on 15/7/29.
//  Copyright (c) 2015年 FangYuan. All rights reserved.
//

#import "EnsureAddressView.h"
#import "EditAddressVC.h"
#define BEIZHU_STR @"备注信息"
//收货地址
#define ADDRESS_URL @"/api/ApiUser/getReceivingAddr"
//编辑收货地址
#define EDIT_ADD_URL @"/api/ApiUser/editReceivingAddr"
@interface EnsureAddressView ()<UITextViewDelegate>
{
    __weak IBOutlet UILabel *nameLab;
    __weak IBOutlet UILabel *phoneLab;
    __weak IBOutlet UILabel *addressLab;
    
    __weak IBOutlet UIButton *ensurebtn;
    __weak IBOutlet UIButton *editbtn;
    
    __weak IBOutlet UILabel *l1;
    __weak IBOutlet UITextView *beizhuText;
    
    // 收货人
    NSString *add_shouhuoren;
    // 联系手机
    NSString *add_mobile;
    // 省
    NSString *add_sheng;
    // 市
    NSString *add_shi;
    // 县
    NSString *add_xian;
    //详细地址
    NSString *add_xiangxi;
    // 街道地址
    NSString *add_jiedao;
    // 收货地址
    NSString *add_addr;
    
    //记着号码
    NSString *_numQQ;
    NSString *_numPhone;
    NSString *_numZFB;
    NSString *currentNum;
}
@end
@implementation EnsureAddressView

-(void)awakeFromNib
{
    //分割线
    [self addSubview:[WYPublic getFenGexian:0 :CGRectGetMaxY(l1.frame) :self.frame.size.width*AUTOX :0.5]];
    [self addSubview:[WYPublic getFenGexian:0 :CGRectGetMaxY(beizhuText.frame)+10 :self.frame.size.width*AUTOX :0.5]];
    [self addSubview:[WYPublic getFenGexian:CGRectGetMaxX(ensurebtn.frame) :CGRectGetMaxY(beizhuText.frame)+10.5 :0.5:40]];
    self.layer.cornerRadius = 2;
    self.layer.masksToBounds = YES;
    [self requestForAddress];
    beizhuText.layer.masksToBounds = YES;
    beizhuText.layer.cornerRadius = 2;
    beizhuText.layer.borderColor = [UIColor lightGrayColor].CGColor;
    beizhuText.layer.borderWidth = 0.5;
    beizhuText.delegate = self;
}

- (IBAction)ensurebtnOnclick:(id)sender {
    if ([self.order_id class] == [NSNull class] || self.order_id == nil) {
        [self.father removeZheZhao];
        [self removeFromSuperview];
        return;
    }
    if ([add_shouhuoren class] == [NSNull class] || add_shouhuoren.length == 0) {
        [self.father removeZheZhao];
        [self removeFromSuperview];
        return;

    }
    if ([add_sheng class] == [NSNull class] ||add_sheng.length == 0) {
        add_sheng = @"";
    }
    if ([add_shi class] == [NSNull class] ||add_shi.length == 0) {
        add_shi = @"";
    }
    if ([add_xian class] == [NSNull class] ||add_xian.length == 0) {
        add_xian = @"";
    }
    if ([add_xiangxi class] == [NSNull class] ||add_xiangxi.length == 0) {
        add_xiangxi = @"";
    }
    if ([_numPhone class] == [NSNull class] ||add_xiangxi.length == 0) {
        _numPhone = @"";
    }
    if ([_numQQ class] == [NSNull class] ||add_xiangxi.length == 0) {
        _numQQ = @"";
    }
    if ([_numZFB class] == [NSNull class] ||add_xiangxi.length == 0) {
        _numZFB = @"";
    }
    NSString *beizhu = beizhuText.text;
    if ([beizhuText.text isEqualToString:BEIZHU_STR]) {
        beizhu = @"";
    }
    //确认地址
    NSDictionary *params = @{@"is_confirm":@"1",@"record_id":self.order_id,@"shouhuoren":add_shouhuoren,@"mobile":add_mobile,@"sheng":add_sheng,@"shi":add_shi,@"xian":add_xian,@"xiangxi":add_xiangxi,@"recharge_mobile":_numPhone,@"recharge_qq":_numQQ,@"recharge_alipay":_numZFB,@"remark":beizhu,@"uid":[UserInfo getUserID],@"token":[UserInfo getUserToken],@"refresh_token":[UserInfo getRefreshToken]};
//    NSLog(@"++++++params = %@",params);
    [WYRequest startPostForDicWithUrlStR:[NSString stringWithFormat:@"%@%@",DBB_IP,EDIT_ADD_URL] Parameters:params successBlock:^(NSDictionary *dic) {
        NSNumber *ret = dic[@"ret"];
        if (ret.intValue == 0) {
           //返回刷新界面
            [self.father fillAddressWith:add_shouhuoren Phone:add_mobile Add:add_addr Andtime:dic[@"data"][@"status_time"]];
            [self.father removeZheZhao];
            [self removeFromSuperview];
        }else{
            if(ret.intValue>2000 && ret.intValue < 3000){
                //用户过期，强制重新登录 先删除用户信息
                [UserInfo deleteUserInfo];
                //登录
                [WYPublic ifUserHaveLogin];
                [WYPublic showMessageWithTitle:@"提示：" Msg:@"登录信息已过期"];
            }else{
                [WYPublic showMessageWithTitle:@"错误提示:" Msg:dic[@"msg"]];
            }
            [self.father removeZheZhao];
            [self removeFromSuperview];
        }
        
        [WYPublic removeZheZhaoToWindow];
    } failedBlock:^{
        [WYPublic removeZheZhaoToWindow];
    } needLog:NO];

    
}
//请求收货地址
- (void)requestForAddress
{
    [WYPublic addHudToWindow];
    NSDictionary *parmars = @{@"uid":[UserInfo getUserID],@"token":[UserInfo getUserToken],@"refresh_token":[UserInfo getRefreshToken]};
    [WYRequest startPostForDicWithUrlStR:[NSString stringWithFormat:@"%@%@",DBB_IP,ADDRESS_URL] Parameters:parmars successBlock:^(NSDictionary *dic) {
        NSNumber *ret = dic[@"ret"];
        NSDictionary *info = dic[@"data"];
        if (ret.intValue == 0) {
            add_addr = info[@"addr"];
            add_jiedao = info[@"jiedao"];
            add_sheng = info[@"sheng"];
            add_shi = info[@"shi"];
            add_xiangxi = info[@"xiangxi"];
            add_xian = info[@"xian"];
            add_mobile = info[@"mobile"];
            add_shouhuoren = info[@"shouhuoren"];
            
            _numQQ = info[@"recharge_qq"];
            _numPhone = info[@"recharge_mobile"];
            _numZFB = info[@"recharge_alipay"];
            //更新界面
            [self updateView];
        }else{
            if(ret.intValue>2000 && ret.intValue < 3000){
                //用户过期，强制重新登录 先删除用户信息
                [UserInfo deleteUserInfo];
                //登录
                [WYPublic ifUserHaveLogin];
                [WYPublic showMessageWithTitle:@"提示：" Msg:@"登录信息已过期"];
            }
            
        }
        
        [WYPublic removeHudToWindow];
    } failedBlock:^{
        
        [WYPublic removeHudToWindow];
    } needLog:NO];
}
- (void)updateView
{
    if ([add_shouhuoren class] != [NSNull class]) {
        nameLab.text = [NSString stringWithFormat:@"收货人:%@",add_shouhuoren];
    }
    if ([add_mobile class] != [NSNull class]) {
        phoneLab.text = [NSString stringWithFormat:@"联系电话：%@",add_mobile];
    }
    if ([add_addr class] != [NSNull class]) {
        addressLab.text = [NSString stringWithFormat:@"地址：%@",add_addr];
    }
}
- (IBAction)editbtnOnclick:(id)sender {
    //去往编辑地址界面
    EditAddressVC *editVC = [[EditAddressVC alloc]init];
    editVC.isZhongJiangFlag = YES;
    editVC.order_id = self.father.model.reward_order_id;
    editVC.lastFooter = self.father;
    if ([add_shouhuoren class] == [NSNull class]) {
        add_shouhuoren = @"";
    }
    if ([add_mobile class] == [NSNull class] || add_mobile == nil) {
        add_mobile = @"";
    }
    if ([add_sheng class] == [NSNull class]|| add_sheng == nil) {
        add_sheng = @"";
    }
    if ([add_shi class] == [NSNull class] || add_shi == nil) {
        add_shi = @"";
    }
    if ([add_xian class] == [NSNull class] || add_xian == nil) {
        add_xian = @"";
    }
    if ([add_xiangxi class] == [NSNull class] || add_xiangxi == nil) {
        add_xiangxi = @"";
    }
    NSString *address = @"";
    if (add_sheng.length != 0) {
        address = [address stringByAppendingString:add_sheng];
    }
    if (add_shi.length != 0) {
        address = [address stringByAppendingString:add_shi];
    }
    if (add_xian.length != 0) {
        address = [address stringByAppendingString:add_xian];
    }
    [editVC MorenInfoWitnName:add_shouhuoren Phone:add_mobile Address:address Detail:add_xiangxi Sheng:add_sheng Shi:add_shi Xian:add_xian];
    [[WYMainTabBarVC shareMainTaBarVC].navigationController pushViewController:editVC animated:YES];

    //去掉当前界面
    [self.father removeZheZhao];
    [self removeFromSuperview];
}
//备注信息代理
- (void)textViewDidBeginEditing:(UITextView *)textView
{
    if ([textView.text isEqualToString:BEIZHU_STR]) {
        textView.text = @"";
        textView.textColor = [UIColor blackColor];
    }
}
- (void)textViewDidEndEditing:(UITextView *)textView
{
    if([textView.text isEqualToString:@""]){
        textView.text = BEIZHU_STR;
    }
}

@end
